//
// Created by LiuYou on 2021/5/7.
//

#include <fcntl.h>
#include <arpa/inet.h>
#include "ClientData.hpp"


int ClientEntry::setNonBlocking(int fd) {
    // 获取文件状态标志。
    int oldOption = fcntl(fd, F_GETFL);
    // 设计新的文件状态是非阻塞的。
    int newOption = oldOption | O_NONBLOCK;
    fcntl(fd, F_SETFL, newOption);
    return oldOption;
}

void ClientEntry::occurrenceTime(FILE* fp, time_t& timeStamp, tm* tp) {
    time(&timeStamp);
    tp = localtime(&timeStamp);
    fprintf(fp, "%d-%d-%d %d:%d:%d []   ", \
             // 因为是从1900年开始计的年份。
            tp->tm_year + 1900, \
             // 因为是从 0 ~ 11 表示的 12个月份。
            tp->tm_mon + 1, \
            tp->tm_mday, \
            tp->tm_hour, \
            tp->tm_min, \
            tp->tm_sec);
}


const sockaddr_in& ClientEntry::ClientData::getSockAddrIn() const {
    return sockAddrIn;
}

void ClientEntry::ClientData::setSockAddrIn(const sockaddr_in& sockAddrIn) {
    ClientData::sockAddrIn = sockAddrIn;
}

//char* ClientEntry::ClientData::getWriteBuffer() const {
//    return writeBuffer;
//}
//
//void ClientEntry::ClientData::setWriteBuffer(char* writeBuffer) {
//    ClientData::writeBuffer = writeBuffer;
//}

const char* ClientEntry::ClientData::getBuffer() const {
    return buffer;
}

char* ClientEntry::ClientData::getBuffer() {
    return buffer;
}

const std::string& ClientEntry::ClientData::getUserName() const {
    return userName;
}

void ClientEntry::ClientData::setUserName(const std::string& userName) {
    ClientData::userName = userName;
}

const char* ClientEntry::ClientData::getIp() {
    ptrIp = ip;
    inet_ntop(AF_INET, &sockAddrIn.sin_addr, ptrIp, INET_ADDRSTRLEN);
    return ip;
}

ClientEntry::ClientData::ClientData() = default;

